
Page({
    data: {
		list:[],
		
		defaultList:[],
		red2:[],
		
		prize:0,

		page:1,
		loadEnd:false
    },
    async onLoad (options) {
        this.data.openID = await getOpenId()
		
        await this.getPeriodWinData();

		this.loadmore();
    },
	loadmore(){
		if( this.data.loadEnd ) return;
		let data = this.setPaginate(this.data.defaultList,10,this.data.page);
		if( !data||!data.length ){
			this.data.loadEnd = true;
			this.setData({ loadEnd:this.data.loadEnd })
			return
		}

		let list = this.data.list;
		list = list.concat(data);
		this.data.page++;
		this.data.list = list;
		this.setData({ list:this.data.list })
	},
	// 设计分页
	setPaginate(list,size,page){
		let start = ( page-1 )*size;
		let end = page*size;
		return list.slice(start,end)
	},

	async getPeriodWinData(){
		return new Promise(async (resolve,reject)=>{
			let defaultList = await request("kl8");
			let { code,data,red2 } = await this.getDay();
			this.setData({ code,data,red2 })

			// // 是否需要更新
			if( !defaultList.some(item=>item.code==code) ){
				defaultList.unshift({ code,data,red2 });
				writeFile(defaultList,"kl8")
			}

			// console.log(defaultList)

			this.data.defaultList = defaultList;
			this.data.prize = defaultList.filter(item=>item.isBuy).length*10

			this.setData({ defaultList:this.data.defaultList,prize:this.data.prize });
			resolve();
		})
	},
	getDay(){
		return new Promise((resolve,reject)=>{
			wx.request({
				url: 'https://caipiao.ip138.com/kuaile8/',
				dataType:"其他",
				responseType:"text",
				enableHttp2:true,
				complete:(res=>{
					let html = res.data.replace(' ','');
					html = html.replace('    ','');
					html = html.replace('                               ','');
					html = html.replace('\n\n','');
					html = html.replace('\n\n','');
					let star = html.indexOf('<span class="period">');
					var period = html.substring(star+21,star+21+7);
					let star2 = html.indexOf('总销售额：<span>');
					var prize = html.substring(Number(star2)+11,Number(star2)+11+11)
					let redball = 'cache.ip138.com/public/image/award/redball-';
					var i1 = html.indexOf(redball);
					var str = html.substr(i1-12,520*3+100)
					str = str.replace(/\/\/cache.ip138.com\/public\/image\/award\/redball-/g,'');
					str = str.replace(/\/\/cache.ip138.com\/public\/image\/award\/blueball-/g,'');
					str = str.replace(/\n/g,'');
					var b=/alt="\d*"/g
					var s=str.match(b)
					let win = []
					for(var i= 0;i<s.length;i++){
						let num = s[i].replace("alt=",'');
						num = num.replace(/"/g,'');
						win.push(num);
					}
					// let data = {
					// 	period:2024004,
					// 	prize:Number(prize),
					// 	win:["2", "16", "18", "29", "27", "8", "9"]
                    // }
					let data = {
						code:Number(period),
						red2:win
                    }
                    console.log("data",data)
					resolve(data)
				})
			})
		})
	}
})